package bl;

import java.util.List;

import models.Book;

public class LibraryFacadeImpl implements LibraryFacade {

	public PaginatedResult<Book> search(String query, int page, int len) {
		final String jpaQuery = 
		" from Book b where lower(b.name) like ? " +
		" or b in (select ab from Author a join a.books ab where lower(a.lastName) like ?)"; 
		
		PaginatedResult<Book> result = new PaginatedResult<Book>();
		result.page = page;
		result.length = len;
		result.items = Book.find(
				"select b "+jpaQuery,
				"%"+query.toLowerCase()+"%",
				"%"+query.toLowerCase()+"%").fetch(page, len);
		result.count = Book.count(
				"select count(b) "+jpaQuery, 
				"%"+query.toLowerCase()+"%",
				"%"+query.toLowerCase()+"%");
		return result;
	}

}
